package cn.com.yang.thread;

import java.util.concurrent.Semaphore;

/**
 * 线程信号量2 TODO
 * 
 * @author yangyijin
 * @since 2023年10月24日-上午9:44:42
 */
public class TestSemaphore2 {
	public static void main(String[] args) {
		Semaphore sem = new Semaphore(4);
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("申请资源");
					sem.acquire();
					System.out.println("获取到资源");
					Thread.sleep(1000);
					sem.release();
					System.out.println("释放资源");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		for (int i = 0; i < 20; i++) {
			Thread t = new Thread(runnable);
			t.start();
		}
	}
}
